using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using MIMPGameLibrary;

namespace MIMPWinForms.Forms
{
    public partial class DamageAssignmentBox : Form
    {
        private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

        public Dictionary<Monster, int> Assignments;

        private AttackResult result;

        private decimal _damageText {
            get { return decimal.Parse(txtDamage.Text); }
            set { txtDamage.Text = value.ToString(); }
        }

        private decimal _nudMax {
            get {
                decimal max = result.TotalDamageDone;
                foreach (object o in lstDamage.Items) {
                    max -= (decimal)o;
                }
                return max;
            }
        }

        private decimal prevNudValue = 0;

        private bool okClicked = false;

        public DamageAssignmentBox(AttackResult attackResult) {
            if (log.IsInfoEnabled) log.Info("Entered DamageAssignmentBox constructor");
            InitializeComponent();
            result = attackResult;
            UpdateList();
            _damageText = result.TotalDamageDone;
            nudAssignment.Maximum = result.TotalDamageDone;
            this.Closing += new CancelEventHandler(DamageAssignmentBox_Closing);
            if (log.IsInfoEnabled) log.Info("Exiting DamageAssignmentBox constructor");
        }

        void DamageAssignmentBox_Closing(object sender, CancelEventArgs e) {
            e.Cancel = !okClicked;
        }

        private void UpdateList() {
            lstMonsters.Items.Clear();
            lstDamage.Items.Clear();
            foreach (KeyValuePair<Monster, int> kvp in result.DamageAssignments) {
                lstMonsters.Items.Add(kvp.Key);
                lstDamage.Items.Add((decimal)(kvp.Value));
            }
        }

        private void btnOK_Click(object sender, EventArgs e) {
            if (log.IsInfoEnabled) log.Info("Entered btnOK_Click");
            okClicked = true;
            Assignments = new Dictionary<Monster, int>();
            for (int i=0; i<lstMonsters.Items.Count; i++) {
                Monster m = (Monster)(lstMonsters.Items[i]);
                int dmg = (int)((decimal)(lstDamage.Items[i]));
                if (log.IsDebugEnabled) log.Debug("Assigning " + dmg + " damage to " + m);
                Assignments.Add(m, dmg);
            }
            this.Close();
            if (log.IsInfoEnabled) log.Info("Exiting btnOK_Click");
        }

        private void nudAssignment_ValueChanged(object sender, EventArgs e) {
            if (lstMonsters.SelectedItem != null && (_damageText > 0 || prevNudValue >= nudAssignment.Value)) {
                lstDamage.Items[lstMonsters.SelectedIndex] = nudAssignment.Value;
                if (prevNudValue > nudAssignment.Value) {
                    _damageText += prevNudValue - nudAssignment.Value;
                } else if (prevNudValue < nudAssignment.Value) {
                    _damageText -= nudAssignment.Value - prevNudValue;
                }
                prevNudValue = nudAssignment.Value;
                btnOK.Enabled = _damageText == 0;
            } else {
                nudAssignment.Value = prevNudValue;
            }
        }

        private void lstMonster_SelectedIndexChanged(object sender, EventArgs e) {
            if (lstMonsters.SelectedItem != null) {
                //nudAssignment.Maximum = result.TotalDamageDone;
                prevNudValue = (decimal)(lstDamage.Items[lstMonsters.SelectedIndex]);
                nudAssignment.Enabled = _damageText > 0 || prevNudValue > 0;
                nudAssignment.Value = prevNudValue;
                //nudAssignment.Maximum = _nudMax;
            } else {
                nudAssignment.Enabled = false;
            }
        }
    }
}